//
//  CJBaseNavigationViewController.m
//  DatangLife
//
//  Created by 娜 on 2018/4/19.
//  Copyright © 2018年 DatangLife. All rights reserved.
//

#import "CJBaseNavigationViewController.h"

#import "UIBarButtonItem+Addition.h"

@interface CJBaseNavigationViewController ()<UINavigationControllerDelegate,UIGestureRecognizerDelegate>
@property (nonatomic, strong) id popDelegate;
@end

@implementation CJBaseNavigationViewController

// 初始化控制器时进行设置
+ (void)initialize
{
    [[UINavigationBar appearance] setTranslucent:NO];
    NSMutableDictionary * color = [NSMutableDictionary dictionary];
    color[NSFontAttributeName] = kBoldFont(16.f);
    color[NSForegroundColorAttributeName] = [UIColor colorWithHexString:kNomalTextBlackColor];
    [[UINavigationBar appearance] setTitleTextAttributes:color];
}

-(UIStatusBarStyle)preferredStatusBarStyle{
    ///这里设置白色
    return UIStatusBarStyleDefault;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _popDelegate = self.interactivePopGestureRecognizer.delegate;
    // 设置navigation的代理方法
    self.delegate = self;
}

// 重写 push 方法中的内容进行自定义
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.childViewControllers.count != 0) {
 
        viewController.hidesBottomBarWhenPushed = YES;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *image = [UIImage imageNamed:@"Common_back_n"];
        button.frame = CGRectMake(0, 0, 20, 20);
        
        // 这里需要注意：由于是想让图片右移，所以left需要设置为正，right需要设置为负。正在是相反的。
        // 让按钮图片右移15
        [button setImageEdgeInsets:UIEdgeInsetsMake(0, -8 * 2, 0, 0)];
        
        [button setImage:image forState:UIControlStateNormal];
        [button addTarget:self action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
        //                button.titleLabel.font = [UIFont systemFontOfSize:16];
        UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithCustomView:button];

        // 设置导航条的按钮
        viewController.navigationItem.leftBarButtonItem = left;
   
    }else {

    }
    [super pushViewController:viewController animated:animated];
}

// 导航控制器跳转完成的时候调用
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    // 显示根控制器
    if (viewController == self.viewControllers[0]) {
        // 还原滑动返回手势的代理
        self.interactivePopGestureRecognizer.delegate = _popDelegate;
    }else{
        // 不是显示根控制器
        // 实现滑动返回功能
        // 清空滑动返回手势代理，就可以实现滑动返回
        self.interactivePopGestureRecognizer.delegate = nil;
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
